package com.jili.service.impl;

import com.jili.req.MailRequest;
import com.jili.service.SendMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * ClassName: SendMailServiceImpl
 * Package com.jili.service.impl
 * Description:
 *
 * @Author: 廖继礼
 * @Create: 2025/2/23
 * @Version: v1.0
 */
@Service
public class SendMailServiceImpl implements SendMailService {
    private static final Logger logger = LoggerFactory.getLogger(SendMailServiceImpl.class);

    @Value("${spring.mail.username}")
    private String sendMailer;

    @Resource
    private JavaMailSender javaMailSender;

    @Override
    public void sendSimpleMail(MailRequest mailRequest) {
        SimpleMailMessage message = new SimpleMailMessage();
        checkMail(mailRequest);
        // 邮件发件人
        message.setFrom(sendMailer);
        //邮件收件人 1或多个
        message.setTo(mailRequest.getSendTo().split(","));
        // 邮件主题
        message.setSubject(mailRequest.getSubject());
        // 邮件内容
        message.setText(mailRequest.getText());
        // 邮件发送时间
        message.setSentDate(new Date());

        javaMailSender.send(message);

        logger.info("发送邮件成功： {} -> {}", sendMailer, mailRequest.getSendTo());
    }

    @Override
    public void sendHtmlMail(MailRequest mailRequest) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        checkMail(mailRequest);
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            // 邮件发件人
            mimeMessageHelper.setFrom(sendMailer);
            // 邮件收件人 1或多个
            mimeMessageHelper.setTo(mailRequest.getSendTo().split(","));
            // 邮件主题
            mimeMessageHelper.setSubject(mailRequest.getSubject());
            // 邮件内容
            mimeMessageHelper.setText(mailRequest.getText(), true);
            // 邮件发送时间
            mimeMessageHelper.setSentDate(new Date());
            String filePath = mailRequest.getFilePath();
            if (StringUtils.hasText(filePath)) {
                FileSystemResource file = new FileSystemResource(new File(filePath));
                String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
                mimeMessageHelper.addAttachment(fileName, file);
            }
            javaMailSender.send(mimeMessage);
            logger.info("发送邮件成功:{}->{}", sendMailer, mailRequest.getSendTo());
        } catch (MessagingException e) {
            logger.error("发送邮件时发生异常！", e);
        }
    }

    private void checkMail(MailRequest mailRequest) {
        Assert.notNull(mailRequest, "邮件请求不能为空");
        Assert.notNull(mailRequest.getSendTo(), "邮件收件人不能为空");
        Assert.notNull(mailRequest.getSubject(), "邮件主题不能为空");
        Assert.notNull(mailRequest.getText(), "邮件收件人不能为空");
    }
}
